package easy;

public class Solution_530 {

    int minSub = Integer.MAX_VALUE;
    int pre = -1;

    /*public int getMinimumDifference(TreeNode root) {
        dps(root);
        Collections.sort(list);
        int minSub = Integer.MAX_VALUE;
        for (int i = 1; i < list.size(); i++) {
            minSub = Math.min(list.get(i) - list.get(i - 1), minSub);
            if (minSub == 0) {
                return 0;
            }
        }
        return minSub;
    }

    public void dps(TreeNode root) {
        if (root != null) {
            list.add(root.val);
            dps(root.left);
            dps(root.right);
        }
    }*/

    public int getMinimumDifference(TreeNode root) {
        dps(root);
        return minSub;
    }

    public void dps(TreeNode node) {
        if (node==null) {
            return;
        }
        dps(node.left);
        if (pre != -1) {
            minSub = Math.min(node.val - pre, minSub);
        }
        pre = node.val;
        dps(node.right);
    }

    public static void main(String[] args) {
        // [1,0,48,null,null,12,49]
        Solution_530 model = new Solution_530();
        Solution_530.TreeNode root = new Solution_530.TreeNode(4);
        root.left = new Solution_530.TreeNode(2);
        root.left.left = new Solution_530.TreeNode(1);
        root.left.right = new Solution_530.TreeNode(3);
        root.right = new Solution_530.TreeNode(6);
        root.right.left = new Solution_530.TreeNode(5);
        root.right.right = new Solution_530.TreeNode(6);

        /*Solution_530.TreeNode root = new Solution_530.TreeNode(1);
        root.left = new Solution_530.TreeNode(0);
        root.right = new Solution_530.TreeNode(48);
        root.right.left = new Solution_530.TreeNode(12);
        root.right.right = new Solution_530.TreeNode(49);*/

        System.out.println(model.getMinimumDifference(root));
    }

    static class TreeNode {

        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
